package com.huluobo.a2212mdmdemo.demo

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

/**
 *  Created by LC on 2025/5/22.
 */

@Composable
fun MyNavigation() {

    //导航控制器
    val navHostController = rememberNavController()

    NavHost(
        //导航控制器
        navController = navHostController,
        //首页地址
        startDestination = "main",
        //声明该导航中所有的页面
        builder = {
            composable("main") { MainPage(navHostController = navHostController) }
            composable("sec/{str}") {
                //跳转传值,注意 上面地址的位置,必须+/  Navigation通过地址中的/来判断传值的内容
                val str = it.arguments?.getString("str").toString()
                SecondPage(navHostController = navHostController, str)
            }
        })
}

@Composable
fun MainPage(navHostController: NavHostController) {
    Text(text = "主页面", modifier = Modifier
        .background(Color.Yellow)
        .clickable {
            navHostController.navigate("sec/123")
        })
}

@Composable
fun SecondPage(navHostController: NavHostController, str: String) {
    Text(text = "第二页面:$str", modifier = Modifier
        .background(Color.Green)
        .clickable {
            //直接跳转到主页面
//            navHostController.navigate("main")
            //返回到上一页面,与back按钮相同的原理
            navHostController.popBackStack()
        })
}